<?php

//Todo 添加文件功能说明

namespace app\index\controller;

use app\index\library\Auth;
use think\Controller;

class Common extends Controller
{
    protected $auth; //Todo 添加注释说明
    protected $checkLoginExclude = ['Login/index','Login/dologin','Login/logout'];// 不受权限系统的控制的地址
    
    protected function _initialize()  // 每次加载页面的时候会加载一次
    {
        $this->auth = Auth::getInstance();

        $controller = $this->request->controller();
        $action = $this->request->action();
        if (in_array($controller.'/'.$action, $this->checkLoginExclude)){  // 如果不受权限控制，直接返回
            return;
        }
        
        if(!$this->auth->isLogin()){      // 如果没登陆，需要登录
            $this->error('请先登录系统','Login/index');
        }

        if(!$this->auth->checkAuth($controller,$action)){      // 如果权限不满足，则无法访问
            $this->error('无权访问');
        }

        $this->view->engine->layout('common/layout');
        $this->assign('layout_menu', $this->auth->menu($controller));

        $userid = $this->auth->getUserid();
        $userName = $this->auth->getUserName();
        $this->assign('layout_login_user', ['uid' => $userid,
            'name' => $userName
        ]);
    }
}